﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Cinchoo.Core.IO;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Xml;

namespace Cinchoo.Core.ETL
{
    [XmlRoot("csvFileImportTask")]
    public class ChoETLCSVFileTableImportTaskDef : ChoETLTaskDef
    {
        [ChoETLTaskDefPropertyInfo("CONNECTION_STRING", IsRequired = true)]
        [XmlAttribute("connectionString")]
        public string ConnectionString { get; set; }

        [ChoETLTaskDefPropertyInfo("TRNSACTION_SCOPE")]
        [XmlAttribute("transactionScope")]
        public bool TransactionScope{ get; set; }

        [ChoETLTaskDefPropertyInfo("TABLE_NAME")]
        [XmlAttribute("tableName")]
        public string TableName{ get; set; }

        [ChoETLTaskDefPropertyInfo("CLEAN_TABLE_SQL")]
        [XmlAttribute("cleanTableSql")]
        public string CleanTableSql { get; set; }

        [ChoETLTaskDefPropertyInfo("IMPORT_REC_SQL")]
        [XmlAttribute("importRecSql")]
        public string ImportRecSql { get; set; }

        [ChoETLTaskDefPropertyInfo("FILE_PATH", IsRequired = true)]
        [XmlAttribute("filePath")]
        public string FilePath { get; set; }

        [ChoETLTaskDefPropertyInfo("SEPERATOR")]
        [XmlAttribute("seperator")]
        public string Seperator { get; set; }

        protected override ChoETLTask Construct(ChoETLWorkflow workflow)
        {
            ChoGuard.ArgumentNotNullOrEmpty(ConnectionString, "ConnectionString");
            ChoGuard.ArgumentNotNullOrEmpty(FilePath, "FilePath");

            if (Seperator.IsNullOrWhiteSpace())
                Seperator = ",";

            if (!TableName.IsNullOrWhiteSpace())
            {
                CleanTableSql = "DELETE FROM {0}".FormatString(TableName);
                using (StreamReader sr = File.OpenText(FilePath))
                {
                    string line = sr.ReadLine();
                    if (!line.IsNullOrWhiteSpace())
                    {
                        int recCount = line.Split(Seperator).Length;
                        ImportRecSql = "INSERT INTO {0} VALUES (".FormatString(TableName);
                        for (int i = 0; i < recCount; i++)
                            ImportRecSql += "{0} @{1}".FormatString(i == 0 ? String.Empty : ",", i);
                        ImportRecSql += ")";
                    }
                    else
                        return ChoETLNullTask.Instance;
                }
            }
            else if (!ImportRecSql.IsNullOrWhiteSpace())
            {
            }
            else
                throw new ArgumentNullException("Missing TABLE_NAME or IMPORT_REC_SQL values.");

            ChoETLTask task = new ChoETLSQLiteConnectionJob(ConnectionString, TransactionScope);
            if (!CleanTableSql.IsNullOrWhiteSpace())
                task = task.ContinueWith(new ChoETLSQLiteCommandJob(ConnectionString, CleanTableSql));
            task = task.ContinueWith(new ChoETLCSVFileLoaderTask(FilePath, Seperator));
            task = task.ContinueWith(new ChoETLSQLiteCommandTask(ConnectionString, ImportRecSql));

            return task; 
        }
    }
}
